<template>
  <div>
<!--    <div v-if="loading">-->
<!--      <Loading></Loading>-->
<!--    </div>-->
    <div :key="$route.params.id">
      <div id="song-detail">
        <div v-if="showSong" id="song-detail-play">
          <div id="bg" :style="{backgroundImage: 'url(' + backUrl + ')'}"></div>
          <div id="song-info1">{{song.songName}}</div>
          <div id="song-info2"> {{song.artistName}}</div>
          <img :src="song.songPic">
          <audio :src="song.songLink" controls="controls" loop="loop"></audio>
          <div id="song-play">
            <div class="play"><span>播放</span></div>
            <div class="play"><span>喜欢</span></div>

          </div>
          <div>
            <!--            歌曲id{{id}}-->
            <!--            {{test}}-->
            <button id="share-button">分享到...</button>
          </div>
        </div>
      </div>
<!--      <div v-if="!showSong">-->
<!--        <no-network></no-network>-->
<!--      </div>-->
      <recommend-song v-if="showSong" showType="like"></recommend-song>
    </div>
  </div>
</template>

<script>
  import {searchSongDetail} from '../api/song'
  import RecommendSong from './RecommendSong.vue'
  import Loading from './Loading.vue'
  import NoNetwork from './NoNetwork.vue'
  import {get} from 'lodash'

  export default {
    name: 'SongDetail',
    components: {
      RecommendSong,
      Loading,
      NoNetwork
    },
    props: ['test'],
    data() {
      return {
        song: {},
        backUrl: '',
        style: {},
        showSong: false,
        loading: true
      }
    },
    mounted() {
      console.log(this.$route.params.id)
      this.getSongDetail()
    },
    methods: {
      async getSongDetail() {
        searchSongDetail(this.$route.params.id).then((data) => {
          console.log('get data')
          let song = get(data, 'data')
          if (song) {
            this.song = get(song, 'data') ? song.data : ''
            this.backUrl = get(song, 'data.songPic') ? song.data['songPic'] : ''
          }
          this.loading = false
        }).catch(() => {
          console.log('捕获到异常')
        }).finally(() => {
          this.showSong = true
        });
      },
      setData(res) {
        let song = res.data;
        //可以使用lodash get
        if (song.code === 1) {
          this.song = song.data
          this.backUrl = song.data['songPic']
          this.showSong = true
          console.log('123')
        }
        this.loading = false
      }
    },
    // beforeRouteEnter(to, from, next) {
    //   searchSongDetail(to.params.id).then(
    //     (res) => {
    //       // next(vm => {
    //       //   // 通过 `vm` 访问组件实例
    //       // })
    //       next(vm => vm.setData(res))
    //     }
    //   )
    //   // this.getSongDetail().then(() => {
    //   //   next()
    //   // })
    // },
    // watch: {
    //   '$route'(to, from) {
    //     // 对路由变化作出响应...
    //     // console.log("move")
    //     this.getSongDetail()
    //   }
    // }
    // beforeRouteUpdate (to, from, next) {
    //   // 在当前路由改变，但是该组件被复用时调用
    //   // 举例来说，对于一个带有动态参数的路径 /foo/:id，在 /foo/1 和 /foo/2 之间跳转的时候，
    //   // 由于会渲染同样的 Foo 组件，因此组件实例会被复用。而这个钩子就会在这个情况下被调用。
    //   // 可以访问组件实例 `this`
    //   next()
    //   this.getSongDetail()
    // }
    watch: {
      // 如果路由有变化，会再次执行该方法
      '$route': 'getSongDetail'
    },
    beforeRouteUpdate(to, from, next) {
      // 在当前路由改变，但是该组件被复用时调用
      // 举例来说，对于一个带有动态参数的路径 /foo/:id，在 /foo/1 和 /foo/2 之间跳转的时候，
      // 由于会渲染同样的 Foo 组件，因此组件实例会被复用。而这个钩子就会在这个情况下被调用。
      // 可以访问组件实例 `this`
      console.log('beforeRouteUpdate hahahhahahahahahahahahhaah')
      next()
    },
  }
</script>
<style scoped>
  #song-detail {
    width: 300px;
    height: 100%;
    margin: 0 auto;
    display: flex;
    align-items: center;
  }

  #bg {
    position: absolute;
    height: 300px;
    width: 100%;
    margin: 0 auto;
    opacity: 0.1;
    background-size: 100%;
  }

  #song-detail-play {
    margin: 0 auto;
    display: flex;
    align-items: center;
    flex-direction: column;

  }

  #song-info1 {
    font-size: 14px;
    margin-top: 10px;
  }

  #song-info2 {
    font-size: 10px;
    color: dimgrey;
    margin-top: 5px;
  }

  #song-detail-play img {
    width: 150px;
    height: 150px;
    margin-top: 15px;
    /*opacity:1;*/
  }

  #song-play {
    display: flex;
    align-items: center;
    height: 30px;
    margin: 30px auto;
    text-align: center;
    /*width: 30px;*/
  }

  #song-play .play {
    width: 30px;
    height: 30px;
    font-size: 12px;
    border-radius: 50%;
    border: darkseagreen solid 1px;
    margin: 0 15px;
  }

  #song-play .play span {
    width: 30px;
    line-height: 30px;
  }

  #share-button {
    height: 36px;
    width: 220px;
    background-color: lightgreen;
    color: white;
    font-size: 12px;
    outline: none;
    border: none;
    border-radius: 18px;
  }
</style>
